package com.orhon.smartcampus.modules.baseinfo.fetcher;


import com.orhon.smartcampus.modules.baseinfo.dao.SchoolcalendarWeekDao;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarDay;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarWeek;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSchoolCalendarDay;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSchoolCalendarWeek;
import com.orhon.smartcampus.modules.baseinfo.mm.SchoolCalendarDayMapper;
import com.orhon.smartcampus.modules.baseinfo.mm.SchoolCalendarWeekMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SchoolCalendarWeekFetcher implements DataFetcher {


	@Autowired
	SchoolcalendarWeekDao schoolcalendarWeekDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from SchoolcalendarWeek where deleted_at = null and id = "+id;
		SchoolcalendarWeek item =  schoolcalendarWeekDao.findId(hql, null);
		if(item!=null) {
			List<GQLSchoolCalendarDay> gqlSchoolCalendarDay= null;
			if (env.getSelectionSet().contains("days")){
				List<SchoolcalendarDay> days = item.getSchoolcalendarDays();
				gqlSchoolCalendarDay = SchoolCalendarDayMapper.MAPPER.toSchoolCalendarDayList(days);
			}

			GQLSchoolCalendarWeek schoolcalendarWeek = SchoolCalendarWeekMapper.MAPPER.toSchoolcalendarWeek(item);
			schoolcalendarWeek.setDays(gqlSchoolCalendarDay);

			return schoolcalendarWeek;
		}
		return null;
	}


}
